#!/usr/bin/env bash

declare frontend_dir

set -e

cd "$(dirname "$0")/.."

if [ ! -f "${PWD}/config/configuration.yaml" ]; then
  mkdir -p "${PWD}/config"
  hass --config "${PWD}/config" --script ensure_config
  echo "Creating default configuration."
  echo "
default_config:
frontend:
  themes: !include_dir_merge_named themes
logger:
  default: info
  logs:
    custom_components.hacs: debug
    # aiogithubapi: debug
    # awesomeversion: debug
" >> "${PWD}/config/configuration.yaml"
fi

while getopts u:a:f: flag
do
    case "${flag}" in
        f) frontend_dir=${OPTARG};;
    esac
done


if [[ -z "${frontend_dir}" ]]; then
  echo "Installing HACS frontend"
  bash "scripts/install/frontend"
fi


# Set the python path to include our custom_components directory
export PYTHONPATH="${PYTHONPATH}:${PWD}/custom_components"

# Start Home Assistant
HACS_FRONTEND_DIR="$(readlink -f ${frontend_dir})" hass --config "${PWD}/config" --debug